﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class WaitingPetMediator extends AbstractActorMediator {

        public var sunModel:SunModel;
        public var tutorialModel:TutorialModel;
        public var computerModel:ComputerModel;
        private var isWalkingToReception:Boolean;// = false
        private var isPicked:Boolean;// = false
        private var animationTimer:Timer;

        private static const MIN_TIMER:int = 5000;
        private static const MAX_TIMER:int = 10000;

        override public function onRegister():void{
            super.onRegister();
            eventMap.mapListener(eventDispatcher, EventConstants.WALK_TO_QUEUE, this.onWalkToQueue);
            eventMap.mapListener(eventDispatcher, EventConstants.TUTORIAL_WALK_TO_QUEUE, this.onTutorialWalkToQueue);
            eventMap.mapListener(eventDispatcher, EventConstants.WALK_TO_RECEPTION, this.onWalkToReception);
            eventMap.mapListener(eventDispatcher, EventConstants.MOVE_UP_IN_QUEUE, this.onMoveUp);
            eventMap.mapListener(this.view, MouseEvent.MOUSE_DOWN, this.onMouseDown);
            this.view.setAnimationState(GameConstants.ANIMATION_STATE_STILL, true);
            this.setAnimationTimer();
        }
        override public function onRemove():void{
            this.animationTimer.stop();
            this.animationTimer.removeEventListener(TimerEvent.TIMER, this.onTimerComplete);
            this.animationTimer = null;
        }
        override protected function updateRenderList(_arg1:Event=null):void{
        }
        override protected function updateRenderListForDragging(_arg1:Event=null):void{
        }
        override protected function storeViewRectangle():void{
            AbstractActorVO(_vo).viewRectangle = this.view.rectangle;
        }
        override protected function setPosition(_arg1:Number, _arg2:Number):void{
            this.view.direction = 1;
            super.setPosition(_arg1, _arg2);
        }
        override protected function onMouseOut(_arg1:MouseEvent):void{
            super.onMouseOut(_arg1);
            dispatch(new InfoEvent(EventConstants.MOUSE_OUT_ON_WAITING_PET, this.vo.id));
        }
        protected function onMouseDown(_arg1:MouseEvent):void{
            var _local2 = (this.tutorialModel.state <= TutorialModel.ROOMTUTORIAL_FINISH_TUTORIAL);
            var _local3 = (this.computerModel.lastClickedPetId == this.vo.id);
            if (((_local2) || (_local3))){
                dispatch(new InfoEvent(EventConstants.DOUBLE_CLICKED_WAITING_PET, null, this.vo));
                this.computerModel.lastClickedPetId = "";
                return;
            };
            var _local4 = (worldModel.patientsWithoutWaiting.length < (worldModel.doctors.length + 7));
            if (_local4){
                dispatch(new InfoEvent(EventConstants.CLICK_ON_WAITING_PET, null, {vo:this.vo, position:new Point(this.view.x, this.view.y)}));
                this.computerModel.lastClickedPetId = this.vo.id;
            } else {
                this.setSunSpeechBubble();
            };
        }
        override protected function stopMoving():void{
            super.stopMoving();
            if (((this.isWalkingToReception) && (!(this.isPicked)))){
                this.pickPatient();
            };
        }
        private function setAnimationTimer():void{
            this.animationTimer = new Timer(5000);
            this.animationTimer.addEventListener(TimerEvent.TIMER, this.onTimerComplete);
            this.animationTimer.delay = Numbers.randomIntBetween(MIN_TIMER, MAX_TIMER);
            this.animationTimer.start();
        }
        private function startTimer(_arg1:int, _arg2:int):void{
            this.animationTimer.delay = Numbers.randomIntBetween(_arg1, _arg2);
            this.animationTimer.start();
        }
        private function onTimerComplete(_arg1:TimerEvent):void{
            var _local2:int = Numbers.randomIntBetween(0, 100);
            var _local3 = GameConstants.ANIMATION_STATE_STILL;
            switch (true){
                case (_local2 < 33):
                    _local3 = GameConstants.ANIMATION_STATE_IDLE_1;
                    break;
                case (_local2 < 66):
                    _local3 = GameConstants.ANIMATION_STATE_IDLE_2;
                    break;
            };
            this.view.setAnimationState(_local3);
            this.startTimer(MIN_TIMER, MAX_TIMER);
        }
        private function onWalkToReception(_arg1:InfoEvent):void{
            if (_arg1.id == this.vo.id){
                this.walkToReception();
            };
        }
        private function onWalkToQueue(_arg1:InfoEvent):void{
            if (_arg1.id == this.vo.id){
                this.walkToWaitingQueue();
            };
        }
        private function onTutorialWalkToQueue(_arg1:Event):void{
            this.walkToWaitingQueue();
        }
        private function onMoveUp(_arg1:InfoEvent):void{
            if (_arg1.id == this.vo.id){
                this.walkToWaitingQueue();
            };
        }
        private function walkToReception():void{
            this.isWalkingToReception = true;
            var _local1:Vector.<Point> = this.getRouteToReception();
            setTargetRoute(_local1);
        }
        private function walkToWaitingQueue():void{
            if (this.isWalkingToReception){
                return;
            };
            var _local1:Vector.<Point> = this.getRouteToWaitingQueue();
            setTargetRoute(_local1);
        }
        private function getRouteToReception():Vector.<Point>{
            var _local1:Array = worldModel.getRoomIdsOfType(GameConstants.TYPE_RECEPTION);
            var _local2:IRoom = (worldModel.getRoomById(_local1[0]) as IRoom);
            var _local3:Point = _local2.getTargetPositionOf(GameConstants.STATE_WAITING_QUEUE);
            var _local4:Vector.<Point> = new Vector.<Point>();
            _local4.push(_local3);
            return (_local4);
        }
        private function getRouteToWaitingQueue():Vector.<Point>{
            var _local1:IRoom = (worldModel.getRoomById(this.vo.roomId) as IRoom);
            var _local2:Point = _local1.getTargetPositionOf(this.vo.id);
            var _local3:Vector.<Point> = new Vector.<Point>();
            _local3.push(_local2);
            return (_local3);
        }
        private function pickPatient():void{
            this.isPicked = true;
            var _local1:IWaitingQueue = (worldModel.getRoomById(this.vo.roomId) as IWaitingQueue);
            var _local2:int = _local1.getPositionInQueue(this.vo.id);
            dispatch(new InfoEvent(EventConstants.PICK_NEW_PATIENT, _local2.toString(), this.vo));
        }
        private function setSunSpeechBubble():void{
            this.sunModel.simpleMessage = languageModel.getTextByKey("reception_layer_nopetfree");
        }
        private function get view():WaitingPetView{
            return ((viewComponent as WaitingPetView));
        }
        private function get vo():WaitingPetVO{
            return ((_vo as WaitingPetVO));
        }

    }
}//package net.wooga.pets.views.world 
